package com.request; /**
 * @author wzm
 * @create 2023-04-25 15:38
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/request01")
public class Request01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       //1.获取请求行中的内容
        //①获取请求的方式
        String method = request.getMethod();  //GET
        System.out.println("method = " + method);

        //②获取项目路径(虚拟目录 项目名)
        String contextPath = request.getContextPath();// /javaweb7
        System.out.println("contextPath = " + contextPath);

        //③获取项目的统一资源定位符 URL
        StringBuffer requestURL = request.getRequestURL();//完整资源路径
        System.out.println("requestURL.toString() = " + requestURL.toString());

        //④获取统一资源标识符
        String requestURI = request.getRequestURI();//资源在当前下哪个位置
        System.out.println("requestURI = " + requestURI);

        //⑤获取请求参数（GET方式）
        String queryString = request.getQueryString();//username=zs&password=123
        System.out.println("queryString = " + queryString);

        //2.获取请求头  key:value
        //user-agent：浏览器版本信息
        String header = request.getHeader("User-Agent");
        System.out.println("header = " + header);
        //请求头比较多根据需求使用key即可


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //3.获取请求体数据  get请求没有请求体 我们需要使用post请求
        System.out.println("post访问了");
        //①获取post请求的请求体数据
        //字符输入流
        BufferedReader reader = request.getReader();
        //需要从流中读取信息
        String s = reader.readLine();
        System.out.println("s = " + s);

    }
}
